/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MVC;

import java.net.UnknownHostException;
import protocol.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

/**
 *
 * @author taillefe
 */
public class Receive extends Thread {

    private ChatController mon_controller;
    private boolean connecte;

    Receive(ChatController aThis) {
        this.mon_controller = aThis;
    }

    public void setconnecte(boolean b) {
        this.connecte = b;
    }

    @Override
    public void run() {

        DatagramSocket socket = null;
        byte[] buf = new byte[2560];
        Message message;


        // Création du socket d'écoute sur le port 2235
        try {
            socket = new DatagramSocket(2235);
            

            while (connecte == true) {

                // Création du datagramme UDP
                DatagramPacket packet = new DatagramPacket(buf, buf.length);

                  try {
                    socket.receive(packet);               
                    //Message m = Receive.deserialiser(packet.getData());
                    /*InetAddress adr = packet.getAddress();
                    mon_controller.traitement_reception(m, adr);*/

                    //il faut récuperer l'adresse de celui qui nous a envoyé !!!
                    InetAddress adr = packet.getAddress();
                    ByteArrayInputStream ohoh = new ByteArrayInputStream(packet.getData());
                    ObjectInputStream recu = new ObjectInputStream(ohoh);
                    try {
                        message = (Message) recu.readObject();
                        System.out.println("addresse :" + adr.toString());
                        System.out.println("message :" + message.toString());
                        try {
                            mon_controller.traitement_reception(message, adr);
                        } catch (UnknownHostException ex) {
                            System.out.println("problemen dans la reception");
                        } catch (InterruptedException ex) {
                            System.out.println("probleme dans la reception2");
                        }
                    } catch (ClassNotFoundException ex) {
                    System.out.println("Problem dans la lecture !");
                      }
                   } catch (IOException ex) {
                System.out.println("Probleme dans le receive !");
                     }

            }

        }
            catch (SocketException ex) {
            System.out.println("Impossible de creer le socket UDP sur le port 2235 !");
            }
        
            socket.close();

    }

    	/**
	 *
	 * @param messToRead le message � d�s�rializer
	 * @return
	 * @return un objet de type Message
	 */
	public static Message deserialiser(byte[] messToRead){
		Message result = null;

		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(messToRead);
			ObjectInputStream ois = new ObjectInputStream(bis);
			result = (Message) ois.readObject();

		} catch (IOException e) {
			System.out.println("Objet non d�s�rialis�");
		} catch (ClassNotFoundException e) {
			System.out.println("Objet non d�s�rialis�");
		}

		return result;

	}
}


